import { observable, action } from 'mobx';
import { Toast } from 'weaver-mobile';
import { ListViewPage, SearchPage } from 'weaver-mobile-page';

import Api from '../../apis/contacts';

const rootPath = '/contacts';

const initState = {
  showCheck: false,
  islight: false,
  showPopover: false,
  showPicker: false,
  searchKey: 'keyword',
  current: 1,
  searchParams: {},
  template: { default: [{ key: 'col', configs: [{ key: 'col_row', configs: [{ key: 'name' }] }] }] },
  routes: {
    writeNew: `${rootPath}/new`,
    addContact: `${rootPath}/addContact`,
    searchPage: `${rootPath}/search`,
    personCard: `${rootPath}/personCard`,
    personCardAddContact: `${rootPath}/personCard/addContact`,
  },
  localeStorageKey: 'searchHistory__email_contacts',
  sysGroups: [],
  userGroups: [],
  mailgroupid: undefined,
  mailContact: {
    id: '',
    mailUserName: '',
    mailaddress: '',
  },
  opt: '',
  concactIds: [],
  currentAction: '',
  newParams: {
    to: '',
    isInternal: 0,
  },
};

export default class ContactStore {

  rootPath = rootPath;
  routes = initState.routes;
  template = initState.template;
  localeStorageKey = initState.localeStorageKey;
  @observable current = initState.current;
  @observable searchKey = initState.searchKey;
  @observable searchParams = initState.searchParams;
  @observable showCheck = initState.showCheck;
  @observable showPopover = initState.showPopover;
  @observable showPicker = initState.showPicker;
  @observable params = initState.params;
  @observable listViewStore = new ListViewPage.Store();
  @observable searchPageStore = new SearchPage.Store({ route: rootPath, id: 'search', template: initState.template });
  @observable sysGroups = initState.sysGroups;
  @observable userGroups = initState.userGroups;
  @observable mailgroupid = initState.mailgroupid;
  @observable mailContact = initState.mailContact;
  @observable opt = initState.opt;
  @observable concactIds = initState.concactIds;
  @observable currentAction = initState.currentAction;
  @observable newParams = initState.newParams;

  @action
  getContactCountList = (refresh = true) => {
    Api.contactCountList().then(res => {
      const { status, sysGroups, userGroups } = res;
      if (status === '1') {
        let mailgroupid = this.mailgroupid;
        if (mailgroupid === undefined && sysGroups.length) { mailgroupid = sysGroups[0].goutId; }
        this.setState({ sysGroups, userGroups, mailgroupid });
        if (refresh) { this.getListData(1); }
      }
    });
  }

  @action
  getListData = (current) => {
    const { listViewStore, mailgroupid, searchParams } = this;
    const { pageSize } = listViewStore;
    if (current === undefined) { current = listViewStore.current; }
    this.getData({
      mailgroupid,
      ...searchParams,
      current,
      pageSize,
    });
  }

  @action
  getData = (params) => {
    this.loading = true;
    try {
      Api.contactList(params).then(action(res => {
        const { status, datas, current, total, pageSize } = res;
        if (status === '1') {
          this.current = current;
          const newState = {
            current,
            isTable: false,
            loading: false,
            haveCheck: true,
            hasMore: total > current * pageSize,
            data: [...(current === 1 ? [] : this.listViewStore.data), ...datas],
          };
          this.listViewStore.setState(newState);
          this.listViewStore.updateDataSource();
        }
        this.loading = false;
      }));
    } catch (e) {
      Toast.info(e, 1);
      this.loading = false;
    }
    return this;
  }

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  clear = () => this.setState(initState)

}
